package com.lovo.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lovo.bean.User;
import com.lovo.bean.vo.ResultVO;
import com.lovo.config.ApplicationContextFactory;
import com.lovo.config.MyApplicationConfig;
import com.lovo.service.UserService;
import com.lovo.service.impl.UserServiceImpl;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    private UserService userService = (UserService) ApplicationContextFactory.getBean("userService");
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String pwd = req.getParameter("pwd");
        System.out.println(username + " " + pwd);
        User user = userService.login(username,pwd);
        resp.setCharacterEncoding("utf-8");
        ObjectMapper objectMapper = new ObjectMapper();
        ResultVO resultVO = new ResultVO();
        if(user == null){
            resultVO.setCode(ResultVO.FAIL);
        }else{
            resultVO.setCode(ResultVO.SUCCESS);
            resultVO.setData(user);
        }
        resp.getWriter().println(objectMapper.writeValueAsString(resultVO));


    }
}
